<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdvertiseImageRepository;
use App\AdvertiserImg;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
class AdvertiseImageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AdvertiserImg());
        $grid->model()->where('status', '=', 2)
            ->orderBy('suggestion', 'desc')
            ->orderBy('resource_created_time', 'desc')
            ->with('advertiser');
//        return Grid::make(new AdvertiseImageRepository(), function (Grid $grid) {
//
//        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableQuickEdit();
            $actions->disableView();
        })->disableActions();

        $grid->disableCreateButton();

        // 这里的字段会自动使用翻译文件
        $grid->column('id')/*->sortable()*/;
        $grid->column('account_id', '广告主帐号');
        $grid->column('advertiser.corporation_name', '广告主名称')->width('125px');
        $grid->column('image_id', '资产id');
        $grid->column('task_id', '易盾taskId');
        $grid->column('suggestion', '审核结果')->filter(
            Grid\Column\Filter\In::make([
                0 => '通过',
                1 => '疑似',
                2 => '不通过'
            ])
        )->display(function ($suggestion){
            $suggestionArr = [
                0 => '通过',
                1 => '疑似',
                2 => '不通过'
            ];
            return $suggestionArr[$suggestion] ?? '';
        });

        $grid->column('label', '审核备注')->display(function ($label){
            $label = $label ? json_decode($label, true) : [];
            $array_column = array_column($label, 'tagName');
            return $array_column ? implode(',', $array_column) : '';
        });
        $grid->column('preview_url', '图片地址')->display(function ($url) {
            return '<a href="javascript:void(0)" onclick="window.open(' .'\''. $url .'\''. ')">' . "查看" . '</a>';
        });
        $grid->column('resource_created_time', '创建时间')->display(function ($time) {
            return date('Y-m-d H:i:s', $time);
        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('account_id', '广告主帐号');
            $filter->like('advertiser.corporation_name', '企业名称');
            $filter->between('resource_created_time', '创建时间')->datetime()->toTimestamp();;
        });

        return $grid;
    }

//    /**
//     * Make a show builder.
//     *
//     * @param mixed $id
//     *
//     * @return Show
//     */
//    protected function detail($id)
//    {
//        return Show::make($id, new AdvertiseImageRepository(), function (Show $show) {
//            // 这里的字段会自动使用翻译文件
//            $show->field('id');
//            $show->field('account_id', '广告主帐号');
//            $show->field('system_status', '状态');
//            $show->field('corporation_name', '企业名称');
//            $show->field('corporate_brand_name', '品牌名称');
//            $show->field('customized_industry', '行业名称');
//        });
//    }
//
//    /**
//     * Make a form builder.
//     *
//     * @return Form
//     */
//    protected function form()
//    {
//        return Form::make(new AdvertiseImageRepository(), function (Form $form) {
//            // 这里的字段会自动使用翻译文件
//            $form->display('id');
//            $form->text('account_id', '广告主帐号');
//            $form->text('system_status', '状态');
//            $form->text('corporation_name', '企业名称');
//            $form->text('corporate_brand_name', '品牌名称');
//            $form->text('customized_industry', '行业名称');
//        });
//    }
}
